<?php 
    //p9m4t5d8

    require_once 'include/funcoes.php';
    checkLogado();
	
	$menu = array("perfil", "busca");
    
    $id_usuario = $_SESSION['usuario']['id'];
    
    if($_POST){
	
//	var_dump($_POST);
	
	extract($_POST);
	
	$nomeSql = "";
	
	if(!empty($nome))
	    $nomeSql = "nome = '".$nome."', ";
	
	$SQL = "UPDATE usuario SET $nomeSql sexo = '$sexo', nascimento = '$nascimentoAno-$nascimentoMes-$nascimentoDia' "; 			 
	$SQL .= "WHERE id = $id_usuario";
	
//	echo $SQL;
	
	mysql_query($SQL);
	
	mysql_query("DELETE FROM meusinteresses WHERE id_usuario = $id_usuario");
	if($interesses)
	foreach ($interesses as $interesse => $value) {
	    mysql_query("INSERT INTO meusinteresses (id_usuario, id_interesse) VALUES ($id_usuario, $value);");	
	}
	if($names){
	    
	    include('include/php/uploadhelper.php');

	    $names = uploadHelper::getVar('names');
	    $files = uploadHelper::getVar('file_names');
	    

	    $source = uploadHelper::decrypt($files[0]);

	    $type = explode(".", $names[0]);
	    $type = $type[count($type)-1];

	    $nomeFoto = "foto$id_usuario.$type";
	    $destiny = "usuarios/fotos/$nomeFoto";
	    
	    
	    //unlink($destiny);
	    
	    if(copy($source, $destiny)){
		$SQL_foto = "UPDATE usuario SET foto = '$nomeFoto' WHERE id = $id_usuario";
		mysql_query($SQL_foto);
	    }
	}
	
	$mensagem = "Dados alterados com sucesso.";
	
	$query = "SELECT * FROM usuario u WHERE id = $id_usuario";
	$result = mysql_query($query);
	$usuario = mysql_fetch_array($result);
	
	$_SESSION['usuario'] = $usuario;
	$_SESSION['usuario']['logado'] = true;
	
	
    }
    
    $retorno = getDadosEditar($id_usuario);
    extract($retorno);
    
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
	
	<title><?=PROJECT_NAME?></title>
	
	<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" href="include/css/milkbox/milkbox.css" media="screen" />	
	<link href="include/css/geral.css" rel="stylesheet" type="text/css" />	
	
	<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
	<script type="text/javascript" src="include/js/mootools-core-1.4.3-full-compat-yc.js"> </script>
	<script type="text/javascript" src="include/js/mootools-more-1.4.0.1.js"> </script>	
	<script src="include/js/milkbox-yc.js"></script>

	<script type="text/javascript">
	    var numeroDeFotos = 1;
	    var multiple = false;
	</script>
    </head>
    <body class="interna">	    
	<?
	    $semIrPara = true;
	    require_once 'include/layoutUp.php';
	?>	
	
	<h2>Editar Meus Dados</h2><br />
	
	<?if(isset ($mensagem)){ ?>
	    <div class="msgsucesso"><?=$mensagem?></div>
	<?}?>

	<form action="" method="post" enctype="multipart/form-data">
	
	<img id="imgPerfil" style="max-width: 200px;" src="./usuarios/fotos/<?=$dados["foto"]?>" alt="<?=$dados["nome"]?>" />
	<a id="linkU" class="color-black underline" href="#">Subir foto</a>
	    <div id="upload" class="perfil"></div>
	
	    <label>Nome</label>
	    <input class="normal borda1" name="nome" maxlength="45" type="text" value="<?=$dados["nome"]?>"/>
	    <br />
	    
	    <?
	    
	    $masc = "checked='checked'";
	    $fem = "";
	    
	    if($dados['sexo'] == 'm')
		$masc = "checked='checked'";
	    
	    if($dados['sexo'] == 'f'){
		$fem = "checked='checked'";
		$masc = "";		
	    }
	    ?>
	    <label>Sexo</label>
	    <input <?=$masc?> name="sexo" type="radio" value="m" class="normal borda1"/> Masculino
	    <input <?=$fem?> name="sexo" type="radio" value="f" class="normal borda1"/> Feminino
	    <br />
	    
	    <label>Data de Nascimento</label>
	    <select name="nascimentoDia" class="normal borda1">
		<?
		    for ($i = 1; $i < 32; $i++) {
			$checked = "";
			if($dados['dia'] == $i)
			    $checked = "selected='selected'";
			
			$value = $i;
			if($i < 10)
			    $value = "0$i";
		?>
			<option value="<?=$value?>" <?=$checked?>><?=$i?></option>		
		<?
		    }
		?>		
	    </select>
	   <?
	      $arr_meses = array(
		  '01' => 'Janeiro',
		  '02' => 'Fevereiro',
		  '03' => 'Março',
		  '04' => 'Abril',
		  '05' => 'Maio',
		  '06' => 'Junho',
		  '07' => 'Julho',
		  '08' => 'Agosto',
		  '09' => 'Setembro',
		  '10' => 'Outubro',
		  '11' => 'Novembro',
		  '12' => 'Dezembro'
	       );
	   ?>
	    <select name="nascimentoMes" class="normal borda1">
		<?
		    foreach($arr_meses as $mes => $nomeMes) {
			$checked = "";
			if($dados['mes'] == $mes)
			    $checked = "selected='selected'";
		?>
		<option <?=$checked?> value="<?=$mes?>"><?=$nomeMes?></option> 
		<?
		    }
		?>
	    </select>
	    <select name="nascimentoAno" class="normal borda1">
		<?
		    for ($i = 1900; $i < 2012; $i++) {
			$checked = "";
			if($dados['ano'] == $i)
			    $checked = "selected='selected'";
		?>
			<option <?=$checked?> value="<?=$i?>"><?=$i?></option>		
		<?
		    }
		?>		
	    </select>
	    <br />
	    
	    <label>Interesses</label>		
	    <? while ($row = mysql_fetch_array($interesses)) {
		$checked = "";
		if($row['id_interesse'] != null)
		    $checked = "checked='checked'";		    
		?>
		<input class="normal" type="checkbox" <?=$checked?> name="interesses[]" value="<?=$row['id']?>" />&nbsp;<?=$row['interesse']?><br />
		<?		    
		} ?>
	    <br />
	    
	    <input class="normal borda1" type="submit" value="Enviar" />
	</form>
	
	<? include 'include/html/uploadFotos.php'; ?>  
	
    </body>
</html>
